﻿#region
/* ===============================================
 * 作者：guopeng           时间：2012/3/11 17:02:56
 * 
 * 
 * 类描述：
 * 
 * 
 * =============================================== */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ueEngine.Biz.Core.AppService.Member;
using ueEngine.Biz.Inf.LogicEntity;

namespace ueEngine.Web.WebLogic
{
    public class AuthorizationAttribute : System.Web.Mvc.AuthorizeAttribute
    {
        private AuthorizeRole role;
        public AuthorizationAttribute(AuthorizeRole role)
        {
            this.role = role;
        }
        protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            if (!httpContext.User.Identity.IsAuthenticated)
            {
                return false;
            }
            if (role == AuthorizeRole.Company)
            {
                return new UserService(httpContext.User.Identity.Name).GetUser().Role == MemberStatus.Firm;
            }
            else
            {
                return new UserService(httpContext.User.Identity.Name).GetUser().Role == MemberStatus.Seeker;
            }
        }
        protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                filterContext.HttpContext.Response.Redirect("/home/index");
            }
            else
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
        }
    }

    public enum AuthorizeRole
    { 
        User,
        Company
    }
}
